package com.kcs.dc.report.model.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.SortedMap;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
import com.kcs.common.utils.Constant;
import com.kcs.common.utils.LogWriter;
import com.kcs.dc.report.model.dao.model.userActiveModel;
import com.kcs.dc.report.model.dao.model.utilGridInfoModel;

public class CommonDAO {
	
	public userActiveModel[] getUserActive(String user_id, String user_name, String menu_scode, Connection conn) throws SQLException {
		
		CallableStatement proc = null;
		ResultSet rs = null;
		userActiveModel model = null;
		userActiveModel[] models = null;
		ArrayList<userActiveModel> arrList = new ArrayList<userActiveModel>();
		
		try {
			proc = conn.prepareCall("{ call qry_user_authorized_proc(?, ?, ?, ?) }");
			
			proc.setString(1, user_id);
			proc.setString(2, user_name); 
			proc.setString(3, menu_scode);
			proc.registerOutParameter(4, Types.INTEGER);
			
			rs = proc.executeQuery();
			while (rs.next()) {
				model = new userActiveModel();
				model.setActive_stauts(rs.getString(3));
				model.setLast_access_time(rs.getString(4));
				
				arrList.add(model);
			}
			
			if (arrList.size() > 0) {
				models = (userActiveModel[]) arrList.toArray(new userActiveModel[arrList.size()]);
				return models;
			}
				
		} catch (SQLException e) {
			LogWriter.error("1.1 commonDAO [Method : getUserActive] : " + e.getMessage(), Constant.LOGGER_APPLICATION);
			throw new SQLException();
		} finally {
			if (proc != null) {
				try {
					proc.close();
				} catch (SQLException e) {
					LogWriter.error("1.2 commonDAO [Method : getUserActive] : " + e.getMessage(), Constant.LOGGER_APPLICATION);
					throw new SQLException();
				}
			}
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					LogWriter.error("1.3 commonDAO [Method : getUserActive] : " + e.getMessage(), Constant.LOGGER_APPLICATION);
					throw new SQLException();
				}
			}
		}
		
		return models;
	}
	
	public utilGridInfoModel getAllUserActive(String user_id, String user_name, String menu_scode, Connection conn) throws SQLException {
		
		CallableStatement proc = null;
		ResultSet rs = null;
		Result result = null;
		String[][] user = null;
		utilGridInfoModel u_model = null;
	
		try {
		
			proc = conn.prepareCall("{ call qry_user_authorized_proc(?, ?, ?, ?) }");
			
			proc.setString(1, user_id);
			proc.setString(2, user_name); 
			proc.setString(3, menu_scode);
			proc.registerOutParameter(4, Types.INTEGER);
					
			rs = proc.executeQuery();
			result = ResultSupport.toResult(rs);
			int column = result.getColumnNames().length;
			int row = result.getRowCount();			
			SortedMap[] rows = result.getRows();
			
			/*Loop set value to String array dimension. */
			u_model = new utilGridInfoModel();
			u_model.setColumn(column);
			u_model.setColunmName(result.getColumnNames());
			if (row > 0){
				// Have result.
				user = new String[row][column]; 
				for (int r = 0; r < row; r++) {	
					SortedMap r_row = rows[r];
					for (int c = 0; c < column; c++) {
						user[r][c] = r_row.get(result.getColumnNames()[c]).toString();
					}				
				}			
			
			u_model.setRow(row);
			u_model.setGridInfo(user);
				
			}		
			
		} catch (Exception e) {
			LogWriter.error("1.1 commonDAO [Method : getAllUserActive] : " + e.getMessage(), Constant.LOGGER_APPLICATION);
			throw new SQLException();
		} finally {
			if (proc != null) {
				try {
					proc.close();
				} catch (SQLException e) {
					LogWriter.error("1.2 commonDAO [Method : getAllUserActive] : " + e.getMessage(), Constant.LOGGER_APPLICATION);
					throw new SQLException();
				}
			}
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					LogWriter.error("1.3 commonDAO [Method : getAllUserActive] : " + e.getMessage(), Constant.LOGGER_APPLICATION);
					throw new SQLException();
				}
			}
		}
		return u_model;
	}	
	
	
	/* public static void main(String args[]) {
		 CommonDAO a = new CommonDAO();
		 try {
			userAllModel user = a.getAllUserActive("", "", "", ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO));

			if (user !=  null){
				System.out.println(user.getColumn());
				System.out.println(user.getRow());
				
				for (int i = 0; i < user.getColunmName().length; i++) {
					if (i > 0) System.out.print(", ");
					System.out.print(user.getColunmName()[i]);
					
				}
				System.out.println("");
				for (int r = 0; r < user.getRow(); r++) {
					for (int c = 0; c < user.getColumn(); c++) {
						if (c > 0) System.out.print(", ");
						System.out.print(user.getUserprofile()[r][c]);
					}
				System.out.println("");
				}
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
	 */
	 
}
